<?php
/**
 * Measurements Controller
 * 
 * @author CRIA Groep 1 <project@markvink.nl>
 * @version 1.0
 * @package controllers
 */
class MeasurementsController extends AppController {
	/**
    * @var string Controller name (faster in PHP 4)
    */
	var $name = 'Measurements';
	
	/**
    * @var arry uses following Components
    */
	var $components = array('Graph');
	
	/**
    * @var array uses following Models
    */
	var $uses = array('Measurement', 'CacheUsage');
	
	/**
	 * Callback functionn beforeFilter
	 * 
	 * Get triggered on init.
	 */
	function beforeFilter() {
		parent::beforeFilter(); 
		
		if($this->action == "laundriesuser_graph") {
			$this->Security->validatePost = false;
		}
	}
	
	/**
	 * Function laundriesuser_add
	 * 
	 * Controller action, only accessible by laundry route
	 * @param integer $laundry laundry_id passed by route settings
	 */
	function laundriesuser_add($laundry) {
		if($this->data) {
			$this->Measurement->create();
			$this->data['Measurement'] = $this->data['Measurement']['date'];
			$this->data['Measurement']['laundry_id'] = $laundry;
			$this->Measurement->set($this->data);
			if($this->Measurement->validates()) {
				$this->Measurement->save();
				$this->redirect(array('controller' => 'measurements', 'action' => 'edit', 'laundry' => $laundry, $this->data['Measurement']['year'], $this->data['Measurement']['month']));
			}	
		}
	}
	
	/**
	 * Private function __setData
	 * 
	 * Set data to view
	 * @param array $measurement measurement data array
	 */
	function __setData($measurement) {
		$productiontypes = $this->Measurement->Production->Productiontype->find('all');
		$additivetypes = $this->Measurement->Usage->Usagetype->find('all', array(
			'conditions' => array('Usagetype.type' => 'additive')    
		));
		$energytypes = $this->Measurement->Usage->Usagetype->find('all', array(
			'conditions' => array('Usagetype.type' => 'energy')    
		));
		$date = mktime(0, 0, 0, $measurement['Measurement']['month'], 1, $measurement['Measurement']['year']);
		
		$data = $this->Measurement->__foreachDataArray($measurement, array('Production', 'Usage'));
		$data['neightbors'] = $this->Measurement->__getNeighbors($measurement);
		$this->set(compact('measurement', 'productiontypes', 'additivetypes', 'energytypes', 'data', 'date'));
	}
	
	/**
	 * Function laundriesuser_edit
	 * 
	 * Controller action, only accessible by laundry route
	 * @param integer $laundry laundry_id passed by route settings
	 * @param integer $year year
	 * @param integer $month month
	 */
	function laundriesuser_edit($laundry, $year = null, $month = null) {
		$measurement = $this->Measurement->findByMeasurementData($laundry, $year, $month);
		$this->__setData($measurement);
		
		if($this->data) {
			foreach($this->data['Measurement']['Production'] AS $id => $amount) {
				if(isset($amount) && is_numeric($amount)) {
					$this->Measurement->Production->update($measurement['Measurement']['id'], $id, $amount);
				}
			}
			foreach($this->data['Measurement']['Usage'] AS $id => $amount) {
				if(isset($amount) && is_numeric($amount)) {
					$this->Measurement->Usage->update($measurement['Measurement']['id'], $id, $amount);
				}
			}
			$this->CacheUsage->createCache($laundry, $year, $month);
			$this->redirect(array('controller' => 'measurements', 'action' => 'edit', 'laundry' => $laundry, $year, $month));
		}
	}
	
	/**
	 * Function laundriesuser_index
	 * 
	 * Controller action, only accessible by laundry route
	 * @param integer $laundry laundry_id passed by route settings
	 */
	function laundriesuser_index($laundry) {
		$measurements = $this->Measurement->find('all', array(
			'conditions' => array(
				'Measurement.laundry_id' => $laundry,
			),
			'order' => array(
				'Measurement.year',
				'Measurement.month',
			)
		));
		$this->set(compact('measurements'));
	}
	
	/**
	 * Function laundriesuser_graph
	 * 
	 * Controller action, only accessible by laundry route
	 * @param integer $laundry laundry_id passed by route settings
	 * @param string $type type of graph
	 */
	function laundriesuser_graph($laundry, $type = 'production', $measurement = null) {		
		$productiontypes = $this->Measurement->Production->Productiontype->find('list');
		$usagetypes = $this->Measurement->Usage->Usagetype->find('list');
		
		$data = $this->Graph->getData();
		$this->set(compact('data','productiontypes', 'usagetypes'));
	}
}
